import { _decorator, Component, Node, v3, Tween, tween, Vec3 } from 'cc';
import { BasicComponet } from '../../Init/Basic/BasicComponet';
import { GlobalEnum } from '../../Init/Config/GlobalEnum';
import { GlobalTmpData } from '../../Init/Config/GlobalTmpData';
import EventManager from '../../Init/Managers/EventManager';
import { EventTypes } from '../../Init/Managers/EventTypes';
const { ccclass, property } = _decorator;

@ccclass('TombCoffin')
export class TombCoffin extends BasicComponet {
    @property(Node)
    effects1: Node = null;
    @property(Node)
    effects2: Node = null;

    @property(Node)
    coffin: Node = null;

    @property(Node)
    models: Node = null;

    @property(Node)
    cover: Node = null;

    @property(Node)
    jumpPos: Node = null;

    effectId = 0;

    onEvents() {

        this.on(EventTypes.CurLevelEvents.ShowCombCoffiAnim, this.onShowCombCoffiAnim, this);
    }

    setData() {
        this.models.active = false;
        this.effectId = 0;
        this.animPos1.set(0, -10, 0);
        this.animPos2.set(0, 0, 0);
        this.models.setPosition(this.animPos1);
        this.cover.setPosition(this.animPos2);

        setTimeout(() => {
            GlobalTmpData.PartyInfo.jumpPos.set(this.jumpPos.worldPosition);
            GlobalTmpData.PartyInfo.coffinPos.set(this.coffin.worldPosition);
        }, 0);
    }

    update(dt: number) {
        this.smookEffects1(dt);
        this.smookEffects2(dt);
    }

    animPos1 = v3();
    animPos2 = v3();
    //上升动画
    moveUpModelsAnim() {
        Tween.stopAllByTarget(this.animPos1);
        this.effectId = 1;
        this.models.active = true;
        tween(this.animPos1).
            to(4, { y: 0 }, {
                easing: 'sineOut', onUpdate: () => {
                    this.models.setPosition(this.animPos1);
                }
            }).
            call(() => {
                this.coverDownAnim();
            }).start();
    }

    //下沉动画
    coverDownAnim() {
        Tween.stopAllByTarget(this.animPos2);
        this.effectId = 2;
        tween(this.animPos2).to(2, { y: -6 }, {
            onUpdate: () => {
                this.cover.setPosition(this.animPos2);
            }
        }).call(() => {
            this.effectId = 0;
            this.animFinished();
        }).start()
    }

    tmpP = v3();
    cd = 0.1;
    curt = 0;
    //烟雾动画1
    smookEffects1(dt) {
        if (this.effectId != 1) return;

        this.curt += dt;
        if (this.curt >= this.cd) {
            this.curt = 0;
            this.cd = Math.random() * 0.1 + 0.1;
            let e = this.effects1.children[Math.floor(Math.random() * this.effects1.children.length)];
            this.tmpP.set(e.worldPosition);
            this.emit(EventTypes.EffectEvents.show2DEffect, { t: GlobalEnum.Effect2DType.ZombieBirth, p: this.tmpP });
        }

    }

    //烟雾动画2
    smookEffects2(dt) {
        if (this.effectId != 2) return;
        this.curt += dt;
        if (this.curt >= this.cd) {
            this.curt = 0;
            this.cd = Math.random() * 0.3 + 0.2;
            let e = this.effects2.children[Math.floor(Math.random() * this.effects2.children.length)];
            this.tmpP.set(e.worldPosition);
            this.emit(EventTypes.EffectEvents.show2DEffect, { t: GlobalEnum.Effect2DType.ZombieBirth, p: this.tmpP });
        }
    }

    animFinished() {
        this.emit(EventTypes.CurLevelEvents.CombCoffiAnimFinished);
    }

    onEnterParty() {

    }

    onShowCombCoffiAnim() {
        this.moveUpModelsAnim();
    }

}

